<?php
namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Support\Myredis;

class BaseController extends Controller
{
    public $redis;

    public function __construct()
    {
        parent::__construct();
        $this->xcx_openid = isset($GLOBALS['xcx_openid']) &&  $GLOBALS['xcx_openid'] ? $GLOBALS['xcx_openid'] : request('xcx_openid');
        $this->wx_openid = isset($GLOBALS['wx_openid']) &&  $GLOBALS['wx_openid'] ? $GLOBALS['wx_openid'] : request('wx_openid');
        $this->shop_id = isset($GLOBALS['shop_id']) &&  $GLOBALS['shop_id'] ? $GLOBALS['shop_id'] : request('sid');
        $this->mid = isset($GLOBALS['mid']) &&  $GLOBALS['mid'] ? $GLOBALS['mid'] : request('mid');
        $this->admin_id = isset($GLOBALS['admin_id']) &&  $GLOBALS['admin_id'] ? $GLOBALS['admin_id'] : request('aid');
        $this->worker_id = isset($GLOBALS['wid']) &&  $GLOBALS['wid'] ? $GLOBALS['wid'] : request('wid');
//        $this->shop_id = request('sid');
//        $this->mid = request('mid');
//        $this->admin_id = request('aid');
        $this->redis = Myredis::getInstance();
    }


    /**
     * 设置一条或者多条数据的状态
     */
    public function anySetStatus()
    {
        $model = str_replace(request()->route()->getPrefix() . '/', '', '/'.request()->getRelativeUriForPath(request()->path()));
        if (strpos($model,'/')) {
            $tem = explode('/',$model);
            $model = $tem[0];
            if (strpos($model,'-')) {
                $arr = explode('-',$model);
                $model = $arr[0].ucfirst($arr[1]);
            }
        }
        $ids = is_array(request('ids')) ? request('ids') : [request('ids')];
        $status = request('status');
        if (empty($ids)) {
            return $this->error('请选择要操作的数据');
        }
        $where['id'] = ['in', $ids];
        switch ($status) {
            case -1 :
                return $this->delete($this->$model, $where, ['success' => '删除成功', 'error' => '删除失败']);
            case 0  :
                return $this->forbid($this->$model, $where, ['success' => '禁用成功', 'error' => '禁用失败']);
            case 1  :
                return $this->resume($this->$model, $where, ['success' => '启用成功', 'error' => '启用失败']);
            default :
                return $this->error('参数错误');
        }
    }

}